package com.lxw.manage_cms.controller;

import com.lxw.api.cms.CmsPreviewControllerApi;
import com.lxw.framework.domain.cms.CmsConfig;
import com.lxw.manage_cms.service.CmsPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/cmsPage")
public class CmsPreviewController implements CmsPreviewControllerApi {
    @Autowired
    CmsPageService cmsPageService;

    @Override
    @GetMapping("/preview/{id}")
    public void preview(@PathVariable String id, HttpServletResponse httpServletResponse) {

        String html = cmsPageService.preview(id);
        try {
            httpServletResponse.setContentType("text/html");
            httpServletResponse.getWriter().write(html);
            httpServletResponse.getWriter().flush();
            httpServletResponse.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
